package com.wsz.logistic.service.impl;

import com.wsz.logistic.bean.Echarts;
import com.wsz.logistic.bean.Type;
import com.wsz.logistic.bean.Userinfo;
import com.wsz.logistic.bean.Warehouse;
import com.wsz.logistic.common.util.R;
import com.wsz.logistic.mapper.TypeMapper;
import com.wsz.logistic.mapper.UserinfoMapper;
import com.wsz.logistic.mapper.WarehouseMapper;
import com.wsz.logistic.service.EchartsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;


@Service
@Transactional
public class EchartsServiceImpl implements EchartsService {
    @Autowired
    private TypeMapper typeMapper;
    @Autowired
    private WarehouseMapper warehouseMapper;
    @Autowired
    private UserinfoMapper userinfoMapper;

    @Override
    public R getBarList() {
        List<Warehouse> list = warehouseMapper.getAll();
        List<Echarts> wList = new ArrayList<>();
        for (Warehouse warehouse : list) {
            Echarts echarts = new Echarts();
            echarts.setCount(warehouse.getCapacity());
            echarts.setText(warehouse.getName());
            wList.add(echarts);
        }
        return R.ok(wList);
    }

    @Override
    public R getPieList() {
        List<Type> list = typeMapper.getAll();
        List<Echarts> tList = new ArrayList<>();
        for (Type type : list) {
            Echarts echarts = new Echarts();
            echarts.setCount(type.getNum());
            echarts.setText(type.getName());
            tList.add(echarts);
        }
        return R.ok(tList);
    }

    @Override
    public R getLineList() {
        List<Userinfo> list = userinfoMapper.getAll();
        List<Echarts> ulist = new ArrayList<>();
        int aNum = 0;
        int uNum = 0;
        int dNum = 0;
        for (Userinfo userinfo : list) {
            if (userinfo.getRole() == 1) {
                aNum++;
            } else if (userinfo.getRole() == 2) {
                uNum++;
            } else if (userinfo.getRole() == 3) {
                dNum++;
            }
        }
        ulist.add(setEcharts("管理员", aNum));
        ulist.add(setEcharts("普通用户", uNum));
        ulist.add(setEcharts("配送员", dNum));
        return R.ok(ulist);
    }

    private Echarts setEcharts(String text, int num) {
        Echarts echarts = new Echarts();
        echarts.setText(text);
        echarts.setCount(num);
        return echarts;
    }
}
